package com.c3w.synchro.member.validator;

import com.c3w.synchro.common.validation.ValidationResult;
import com.c3w.synchro.common.validation.Validator;
import com.c3w.synchro.common.validation.event.ValidationFailedException;
import com.c3w.synchro.member.bean.ContactInfo;
import org.apache.commons.validator.routines.EmailValidator;
import org.springframework.stereotype.Repository;

/**
 * User: magirard
 * Date: 13-02-09
 */
@Repository("contactInfoValidator")
public class ContactInfoValidator implements Validator<ContactInfo> {
    @Override
    public void validate(ContactInfo bean) throws ValidationFailedException {
        ValidationResult result = new ValidationResult();
        if (bean != null) {

            // 1 - Validation de l'adresse
            String address = bean.getAddress();
            if (address != null) {
                //result.add("age", "Age must be provided");
            }

            // 2 - Validation du email
            String email = bean.getEmail();
            if (email != null) {
                EmailValidator emailValidator = EmailValidator.getInstance();
                if (!emailValidator.isValid(email)) {
                    result.add("email", "Le email est invalide");
                }
            }

//            String phoneNumber = bean.getPhoneNumber();
//            if (phoneNumber != null) {
//                Pattern pattern = Pattern.compile("/^(\\+\\d)*\\s*(\\(\\d{3}\\)\\s*)*\\d{3}(-{0,1}|\\s{0,1})\\d{2}(-{0,1}|\\s{0,1})\\d{2}$/");
//                Matcher matcher = pattern.matcher(phoneNumber);
//
//                if (!matcher.matches()) {
//                    result.add("phoneNumber", "Le numero de telephone est invalide");
//                }
//
//            }

            if (result.getErrorCount() > 0) {
                throw new ValidationFailedException(result);
            }
        }
    }
}